package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Housing;
import com.woniuxy.service.HousingService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.RespongseData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/housing")
@AllArgsConstructor
public class HousingController {
    private HousingService housingService;

    @GetMapping("/housingPage")
    public String userPage(){
        return "housing/housing";
    }

    @GetMapping("/queryHouse")
    @ResponseBody
    public RespongseData queryHouse(){
        List<Housing> housings = housingService.queryHouse();
        return new RespongseData(200,"ok",housings);
    }

    @PostMapping("/queryHouses")
    @ResponseBody
    public RespongseData queryHouses(@RequestBody PageData<Housing> pageData){
        PageInfo<Housing> housingPageInfo = housingService.queryHouses(pageData);
        return new RespongseData(200,"ok",housingPageInfo);
    }

    @PostMapping("/addHouse")
    @ResponseBody
    public RespongseData addHouse(@RequestBody Housing housing){
        housingService.addHouse(housing);
        return new RespongseData(200,"ok",null);
    }

    @PostMapping("/batchDelHouse")
    @ResponseBody
    public RespongseData batchDelHouse(@RequestBody List<Integer> ids){
        boolean b = housingService.batchDelHouse(ids);
        return new RespongseData(200,"ok",b);
    }

    @GetMapping("/updataHouseBtn/{id}")
    @ResponseBody
    public RespongseData updataHouseBtn(@PathVariable("id") Integer id){
        Housing housing = housingService.queryHouseById(id);
        return new RespongseData(200,"ok",housing);
    }

    @PostMapping("/saveUpdateHouse")
    @ResponseBody
    public RespongseData saveUpdateHouse(@RequestBody Housing housing){
        housingService.saveUpdateHouse(housing);
        return new RespongseData(200,"ok",null);
    }

    @GetMapping("/delHouse/{id}")
    @ResponseBody
    public RespongseData delHouse(@PathVariable("id") Integer id){
        boolean b = housingService.delHouse(id);
        return new RespongseData(200,"ok",b);
    }
}
